package com.xiaoyuanxing.demo.controller;

import com.github.pagehelper.Page;
import com.xiaoyuanxing.demo.entity.Talk;
import com.xiaoyuanxing.demo.entity.TalkComment;
import com.xiaoyuanxing.demo.pojo.PageInfo;
import com.xiaoyuanxing.demo.service.TalkService;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.sql.Timestamp;

/*
 **@author LuoShengYing
 **@date 2021/5/28
 */
@RestController
@RequestMapping("Talk")
public class TalkController {
    @Resource(name = "talkService")
    private TalkService talkService;

    /*
    * 查询全部Talk数据(根据最新创建时间排序)
    * */
    @RequestMapping("getTalks")
    public PageInfo<Talk> getTalks(@RequestParam(defaultValue = "1",value = "pageNum") int pageNum,
                                   @RequestParam(defaultValue = "10",value = "pageSize") int pageSize){
        Page<Talk> talks = talkService.getAllTalks(pageNum, pageSize);
        return new PageInfo<>(talks);
    }

    /*
    * 新建Talk
    * */
    @RequestMapping("createNewTalk")
    public boolean createNewTalk(String openid, String title, @RequestParam(defaultValue = "-1") String tag1,
                                 @RequestParam(defaultValue = "-1") String tag2, @RequestParam(defaultValue = "-1") String tag3, String content){
        Talk newTalk = new Talk();
        newTalk.setOpenID(openid);
        newTalk.setTitle(title.trim());
        newTalk.setCreatedTime(new Timestamp(System.currentTimeMillis()));
        newTalk.setTag1(tag1.trim());
        newTalk.setTag2(tag2.trim());
        newTalk.setTag3(tag3.trim());
        newTalk.setContent(content.trim());
        return talkService.createNewTalk(newTalk);
    }

    /*
    * 根据talkID查询此用户动态共有多少评论
    * */
    @RequestMapping("getCommentCount")
    public int getCommentCount(int talkID){
        return talkService.getCommentCount(talkID);
    }

    /*
    * 根据talkID，查询此用户动态的全部评论
    * */
    @RequestMapping("getComments")
    public PageInfo<TalkComment> getComments(int talkID,@RequestParam(defaultValue = "1",value = "pageNum") int pageNum,
                                             @RequestParam(defaultValue = "10",value = "pageSize") int pageSize){
        Page<TalkComment> comments = talkService.getComments(talkID, pageNum, pageSize);
        return new PageInfo<>(comments);
    }

    /*
    * 新建TalkComment
    * */
    @RequestMapping("createNewComment")
    public boolean createNewComment(int talkID, String commentatorOpenID, String comment){
        TalkComment talkComment = new TalkComment();
        talkComment.setTalkID(talkID);
        talkComment.setCommentatorOpenID(commentatorOpenID);
        talkComment.setCommentTime(new Timestamp(System.currentTimeMillis()));
        talkComment.setComment(comment);
        return talkService.createdNewComment(talkComment);
    }

    /*
    * 删除Talk
    * */
    @RequestMapping("deleteTalk")
    public boolean deleteTalk(int talkID){
        return talkService.deleteTalkComments(talkID) && talkService.deleteTalk(talkID);
    }
}
